<template>
  <div id="app">
    <a-input 
      placeholder="请输入任务" 
      class="my_ipt"
      :value="inputVal"
      @change="changeInputVal" />
    <a-button type="primary" @click="addItemToList">添加事项</a-button>

    <a-list bordered :dataSource="changeList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <!-- <a-checkbox :checked="item.done" @click="changeDone(item.id)">{{item.info}}</a-checkbox> -->
        <a-checkbox :checked="item.done" @click="(e) => {changeDone(e, item.id)} ">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="delItemById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneNum}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="nowType === 'all' ? 'primary' : 'default'" @click="changeType('all')">全部</a-button>
          <a-button :type="nowType === 'undone' ? 'primary' : 'default'" @click="changeType('undone')">未完成</a-button>
          <a-button :type="nowType === 'done' ? 'primary' : 'default'"  @click="changeType('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'

export default {
  name: 'app',
  data() {
    return {
    }
  },
  created() {
    this.$store.dispatch('getListData')
  },
  computed: {
    ...mapState(['list', 'inputVal', 'nowType']),
    ...mapGetters(['unDoneNum', 'changeList'])
  },
  methods: {
    changeInputVal(e) {
      this.$store.commit('setInputVal', e.target.value)
    },
    addItemToList() {
      if(this.inputVal.trim().length <= 0) {
        this.$message.info('请输入内容')
        return false
      }

      this.$store.commit('addItem')
    },
    delItemById(id) {
      this.$store.commit('removeItem', id)
    },
    changeDone(e, id) {
      let obj = {
        done: e.target.checked,
        id
      }

      this.$store.commit('changeDone', obj)
    },
    clearDone() {
      this.$store.commit('clearDone')
    },
    changeType(type) {
      this.$store.commit('changeType', type)
    }
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>